Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | /** * API route for per-student session preferences * * GET /api/curriculum/[playerId]/session-preferences * PUT /api/curriculum/[playerId]/session-preferences * * Persists StartPracticeModal settings per student so they survive * across modal opens and browser refreshes. */ import { eq } from 'drizzle-orm' import { NextResponse } from 'next/server' import { db } from '@/db' import { playerSessionPreferences } from '@/db/schema/player-session-preferences' import type { PlayerSessionPreferencesConfig } from '@/db/schema/player-session-preferences' import { withAuth } from '@/lib/auth/withAuth' import { canPerformAction } from '@/lib/classroom' import { getUserId } from '@/lib/viewer' export const GET = withAuth(async (_request, { params }) => { try { const { playerId } = (await params) as { playerId: string } if (!playerId) { return NextResponse.json({ error: 'Player ID required' }, { status: 400 }) } const userId = await getUserId() const canView = await canPerformAction(userId, playerId, 'view') if (!canView) { return NextResponse.json({ error: 'Not authorized' }, { status: 403 }) } const row = await db .select() .from(playerSessionPreferences) .where(eq(playerSessionPreferences.playerId, playerId)) .get() const parsed = row ? (JSON.parse(row.config) as PlayerSessionPreferencesConfig & { euclidLanguageStyle?: PlayerSessionPreferencesConfig['kidLanguageStyle'] }) : null if (parsed && !parsed.kidLanguageStyle && parsed.euclidLanguageStyle) { parsed.kidLanguageStyle = parsed.euclidLanguageStyle } return NextResponse.json({ preferences: parsed, }) } catch (error) { console.error('Error fetching session preferences:', error) return NextResponse.json({ error: 'Failed to fetch session preferences' }, { status: 500 }) } }) export const PUT = withAuth(async (request, { params }) => { try { const { playerId } = (await params) as { playerId: string } if (!playerId) { return NextResponse.json({ error: 'Player ID required' }, { status: 400 }) } const userId = await getUserId() const canEdit = await canPerformAction(userId, playerId, 'start-session') if (!canEdit) { return NextResponse.json({ error: 'Not authorized' }, { status: 403 }) } const body = (await request.json()) as { preferences: PlayerSessionPreferencesConfig } const config = JSON.stringify(body.preferences) const now = Date.now() await db .insert(playerSessionPreferences) .values({ playerId, config, updatedAt: now }) .onConflictDoUpdate({ target: playerSessionPreferences.playerId, set: { config, updatedAt: now }, }) return NextResponse.json({ preferences: body.preferences }) } catch (error) { console.error('Error saving session preferences:', error) return NextResponse.json({ error: 'Failed to save session preferences' }, { status: 500 }) } }) |